<?php

declare(strict_types=1);

namespace app\works;

use Exception;
use owns\facade\OwnsLog;
use owns\library\WorkerCrontab;
use think\worker\Server;
use Workerman\Lib\Timer;

/**
 * 定时任务
 */
class Cron extends Server
{
    protected $socket = 'tcp://0.0.0.0:9527';

    protected $option = [
        'count' => 1,
        'name' => 'owns_tp_crontab'
    ];

    protected $interval = 3;

    protected $crontab;

    public function onWorkerStart()
    {
        $this->crontab = WorkerCrontab::instance();
        $this->crontab->loading();

        Timer::add($this->interval, function() {
            try {
                $this->crontab->run();
            } catch (Exception $e) {
                OwnsLog::log('warning', $e->getMessage(), 'crontab');
            }
        });
    }

}
